package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.adapter.CommentListAdapter
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.CommentListEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.CommentListContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.CommentListPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ActivityCloseUtils
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_comment_list.*

/**
 * Created by JacobHHH on 2018/4/20.
 * 评论列表
 */
class CommentListActivity : BaseMvpActivity<CommentListContract.Model,CommentListContract.View,CommentListPresenter>(),CommentListContract.View {

    private lateinit var mAdapter : CommentListAdapter
    private lateinit var mShop_id : String
    private var currentPage : Int = 1

    override fun createPresenter(): CommentListPresenter = CommentListPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_comment_list

    override fun initData() {
        mShop_id=intent.getStringExtra("shop_id")
        initView()
        getPresenter().loadData(mShop_id,currentPage.toString(),false)
    }

    private fun initView() {
        rec_comment.layoutManager=LinearLayoutManager(mContext)
        mAdapter = CommentListAdapter(R.layout.item_comment_list, ArrayList())
        rec_comment.adapter=mAdapter
        mAdapter.setOnLoadMoreListener {
            getPresenter().loadData(mShop_id,currentPage.toString(),true)
        }
    }

    override fun initEvent() {
        rec_comment.addOnItemTouchListener(object : OnItemClickListener(){
            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                val intent = Intent(mContext, CommentDetailsActivity::class.java)
                intent.putExtra("comment_id",mAdapter.data[position].id.toString())
                startActivityForResult(intent,Constants.ACTIVITY_FINISH_REQUEST_CODE)
                (mContext as Activity).overridePendingTransition(R.anim.translate_in,R.anim.scale_out2)
            }
        })

        ActivityCloseUtils.getInstance()!!.finishActivity(this,rec_comment)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==Constants.ACTIVITY_FINISH_REQUEST_CODE){
            if (resultCode==Constants.ACTIVITY_FINISH_RESULT_CODE){
                finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCloseUtils.getInstance()!!.detach()
    }

    override fun setData(data: List<CommentListEntity>?, isLoadMore: Boolean) {
        currentPage++
        fl_loading!!.visibility=View.GONE
        if (!isLoadMore){
            if(data!!.isEmpty()){
                mAdapter.emptyView = getEmptyView()
            }else{
                mAdapter.setNewData(data)
            }

        }else{
            if (data!=null&& data.isNotEmpty()){
                mAdapter.loadMoreComplete()
                mAdapter.addData(data)
            }else{
                mAdapter.loadMoreEnd()
            }
        }
    }

    override fun onDataFailure(err: String?) {
        fl_loading.visibility=View.GONE
        mAdapter.loadMoreFail()
    }

    fun getEmptyView() : View?{
        return View.inflate(mContext,R.layout.view_empty,null)
    }
}